package dk.mil.triton.hest;

/**
 * The game logic class for the Hest game
 */
public class Game {

    private int secret = -1; //initialization value for class
    private int upper = 100;
    private int lower = 0;

    /**
     * Starts a new game.
     */
    public void newGame() {
        secret = 1 + (int) Math.floor(Math.random() * 99);
        upper = 100;
        lower = 0;
    }

    public int getUpper() {
        return upper;
    }

    public int getLower() {
        return lower;
    }

    /**
     * Validates the guess - range check.
     * <p/>
     * Checks if the guess is between the upper and lower limit.
     *
     * @param theGuess The guess to validate.
     * @return true if it's a valid guess, false otherwise
     */
    public boolean validGuess(int theGuess) {
        return theGuess > lower && theGuess < upper;
    }

    /**
     * Guess on a number
     *
     * @param theGuess The number to guess on.
     * @return true if the guess was correct, false otherwise.
     */
    public GuessResult guess(int theGuess) {
        if (theGuess == secret) {
            return GuessResult.CORRECT;
        } else if (theGuess > secret) {
            if (theGuess < upper) {
                upper = theGuess;
            }
            return GuessResult.TOO_HIGH;
        } else {
            if (theGuess > lower) {
                lower = theGuess;
            }
            return GuessResult.TOO_LOW;
        }
    }
}
